#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "iostream"
#include "QFileDialog"
#include "QBitmap"
#include "QMessageBox"
#include "QDebug"
#include<windef.h>
#include "wingdi.h"
#include <chimage.h>
using namespace std;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_btnExtract_clicked()
{
    extractImage();
    QString s1 = QString(image->getExtract());
    ui->insertArea->setText(s1);
}

void MainWindow::on_btnInsert_clicked()
{
    QString x = ui->insertArea->toPlainText();
    char* toInsert;
    QByteArray ba = x.toLocal8Bit();
    toInsert = ba.data();
    if(strlen(toInsert) > image->getSizeByAttributes() / 8){
        this->ui->insertArea->setText("插入字符超过最大插入长度!");
    }else{
         insertMessage(toInsert);
         ui->insertArea->setText("");
         QMessageBox::information(this,tr("插入信息成功"),tr("插入信息成功"));
    }

}

void MainWindow::on_btnSave_clicked()
{
    QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), "./未命名.bmp", tr("Images (*.bmp)"));
    image->save(filename);
}

//读取图片
void MainWindow::on_btnRead_clicked()
{
    QString filename = QFileDialog::getOpenFileName();
    //显示图片
    if(filename.isEmpty()){
        QMessageBox::information(this,tr("图像不存在"),tr("图像不存在!"));
        return;
    }else{
        QImage* img=new QImage;
        if(! ( img->load(filename) ) ) //加载图像
               {
                   QMessageBox::information(this,tr("打开图像失败"),tr("打开图像失败!"));
                   delete img;
                   //return;
               }
               ui->imageBefore->setPixmap(QPixmap::fromImage(*img));
    }
    //处理
    image= new ChImage(filename);
    if(image->checkImage()){
            ui->log->append("Bitmap successfully loaded.");
            ui->log->append("width:"+QString::number(image->getImageWidth())+" height:"+QString::number(image->getImageHeight())+" bitcount:"+QString::number(image->getBitCount()));
            ui->log->append("can insert:"+QString::number(image->getSizeByAttributes()/8)+" bytes of info at most.");
    }else{
        ui->log->append("Bitmap load failed.");
        return;
    }
}

void MainWindow::on_pushButton_clicked()
{
    image->addNoise();
}
